package com.qf.serviceimpl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 * 号段补全模块
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/11/10 10:51
 */
@SuppressWarnings("all")
@Service(value = "phase")
@Slf4j
public class PhaseFilterServiceImpl implements FilterChainService {

    @Autowired
    CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.debug("【号码补全策略】 开始执行");
        //获取手机号
        String destMobile = standardSubmit.getDestMobile();

        /**
         * 判断运营商
         */
        // 判断 是否是 移动
        if(CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }else if(CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //联通
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        }else if(CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            // 电信
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
        }else{
            //未知的
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
        }

        /**
         * 判断省市
         */
        //去首尾空格
        destMobile = destMobile.trim();
        //获取手机号的前7位 (包含0  不包含下标为7的)
        String phase = destMobile.substring(0,7);

        //从redis中获取 归属地
        String homeLocation = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + phase);
        //切割
        String[] split = homeLocation.split(",");
        //获取省
        standardSubmit.setProvinceId(Integer.parseInt(split[0]));
        //获取市
        standardSubmit.setCityId(Integer.parseInt(split[1]));

        //获取运营商通道
        Map map = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL + standardSubmit.getOperatorId() + ":" + standardSubmit.getGatewayID());
        String spNumber = String.valueOf(map.get("spnumber"));

        //获取企业通道
        Map map1 = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID());
        if(map1 == null || map1.size() == 0){
            log.debug("【号码补全策略】 没有查询到此通道");
            return false;
        }
        String extendNumber = String.valueOf(map1.get("extendnumber"));
        //下发源号码
        standardSubmit.setSrcNumber(spNumber + extendNumber);

        //获取 目标手机号对应的运营商 对应的扣费
        long price =Long.parseLong(String.valueOf(map1.get("price")));
        standardSubmit.setFee(price);

        log.debug("【号码补全策略】 执行成功");
        return true;
    }
}
